package com.cssw.fyzb.core.dao.pay;

import com.cssw.fyzb.core.model.dodb.PayOrder;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author zhouxu
 * @create 2020-12-18 22:18
 */
@Repository
public class PayOrderDaoImpl implements PayOrderDao {

    @Override
    public PayOrder createOrder(Long userId, Integer payAmount, String payBusi, String outTradeNo, String payWay, BigDecimal fCoinNum) {
        PayOrder order = new PayOrder();
        order.setUserId(userId);
        order.setPayAmount(payAmount);
        order.setPayBusi(payBusi);
        order.setOutTradeNo(outTradeNo);
        order.setStatus(PayOrder.STATUS_INIT);
        order.setPayWay(payWay);
        order.setFCoinNum(fCoinNum);
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        order.save();
        return order;
    }

    @Override
    public void updateStatus(PayOrder order, String status) {
        if (order != null) {
            order.setStatus(status);
            order.setUpdateTime(new Date());
            order.update();
        }

    }

    @Override
    public void updateStatusOrder(PayOrder order, String status, String outTradeNo) {
        order.setStatus(status);
        order.setOutTradeNo(outTradeNo);
        order.setUpdateTime(new Date());
        order.update();
    }

    @Override
    public PayOrder findByTradeNo(String tradeNo) {
        String sql = " select * from t_pay_order where out_trade_no = ? ";
        return PayOrder.dao.findFirst(sql, tradeNo);
    }
}
